package com.litbw.user;
/*
 * ImageViewer.java
 */

import javax.swing.*;

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.net.*;

/**
 * This class will be used in association with the draft, 
 * it represents a panel that can be added to the main frame 
 * of other container once the application is ready to display 
 * the draft. 
 * @author Derrick Mink
 *
 */
@SuppressWarnings("serial")
public class ImageViewer extends JPanel {
	private static int HEIGHT;
	private static int WIDTH;
	private BufferedImage image;
	private JLabel area = new JLabel();


	public ImageViewer(int width, int height) {
		WIDTH = width;
		HEIGHT = height;
		setLayout(new BorderLayout());
		add(area, BorderLayout.CENTER);
	}

	public boolean readInFile(String fileName, int type) {
		File file = new File(fileName);
		//image
		if (type == 1) {
			try {
				image = ImageIO.read(file);
			} catch (IOException e) {
				//do something
			}
		//WIF file
		}else if (type == 2){
				
			
		//URL
		} else {
			URL url = getClass().getResource(fileName);
			if (url == null) {
				try {
					url = new URL(fileName);
				} catch (MalformedURLException e) {
					//do something
				}
			}
			try {
				image = ImageIO.read(url);
			} catch (IOException e) {
				//do something
			}
		}
		
		if(image == null){
			return false;
		} else {
			return true;
		}
	}

	public void setImage() {
		ImageIcon icon = new ImageIcon(image);
		int height = icon.getIconHeight(); 
		int width = icon.getIconWidth();
		while(height > HEIGHT && width > WIDTH) {
			height = height / 2;
			width = width / 2;
		}
		Image image = icon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
		icon.setImage(image);		
		area.setIcon(icon);
		setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
	}


}